//
//  LHYVisitorView.m
//  rabbit微博
//
//  Created by mac on 15/12/15.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "LHYVisitorView.h"

@interface LHYVisitorView ()

@property (nonatomic,strong)UIImageView *largeView;
@property (nonatomic,strong)UIImageView *backView;
@property (nonatomic,strong)UIImageView *cycleView;
@property (nonatomic,strong)UIButton *registerBtn;
@property (nonatomic,strong)UIButton *loginBtn;
@property (nonatomic,strong)UILabel *tipLable;

@end

@implementation LHYVisitorView

- (instancetype)init
{
   self = [super init];
    
    [self addView];

    return self;
}

- (void)visitorView:(NSString *)tipText andWithImageName:(NSString *)imageName
{
    //让所选的控制器的View上面的label文字为tipText
    self.tipLable.text = tipText;
    
    if (imageName) {
        
        //让所选的控制器的View上面的图片为imageName
        self.cycleView.image = [UIImage imageNamed:imageName];
        
        //隐藏大房子图片
        self.largeView.hidden = YES;
        
        //让cycleView的图片永远在最前端显示
        [self bringSubviewToFront:self.cycleView];
    }
    else
    {
        //执行动画
        [self startAnimation];
    }
    
}
#pragma mark --- 添加周围的动画
- (void)startAnimation
{
    //基础动画
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    anim.repeatCount = MAXFLOAT;
    anim.duration = 15.0;
    anim.toValue = @(2 * M_PI);
    anim.removedOnCompletion = NO;
    
    [self.cycleView.layer addAnimation:anim forKey:nil];
}

#pragma mark ---添加页面上的控件
/*******************添加控件的方法以及添加控件的frame*********************/
- (void)addView
{
    [self addSubview:self.cycleView];
    [self addSubview:self.backView];
    [self addSubview:self.largeView];
    [self addSubview:self.tipLable];
    [self addSubview:self.registerBtn];
    [self addSubview:self.loginBtn];
    
    
    for (UIView *subview in self.subviews) {
        subview.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    //添加大图片的自动布局
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.largeView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.largeView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:-60]];
    
    //添加圆圈的自动布局
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.cycleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.largeView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.cycleView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.largeView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
    
     //添加lable的自动布局
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLable attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.cycleView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLable attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.cycleView attribute:NSLayoutAttributeBottom multiplier:1 constant:16]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLable attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:230]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.tipLable attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:40]];
    
    //添加registerBtn的自动布局
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.tipLable attribute:NSLayoutAttributeRight multiplier:1 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tipLable attribute:NSLayoutAttributeBottom multiplier:1 constant:20]];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:35]];
    
    //添加loginBtn的自动布局
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.tipLable attribute:NSLayoutAttributeLeft multiplier:1 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tipLable attribute:NSLayoutAttributeBottom multiplier:1 constant:20]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:100]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginBtn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:35]];
    
    //添加backView的自动布局
    NSArray *layoutA = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[backView]-0-|" options:0 metrics:nil views:@{@"backView": self.backView}];
    [self addConstraints:layoutA];
    
    NSArray *layoutB = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[backView]-(-35)-[regBtn]" options:0 metrics:nil views:@{@"backView":self.backView,@"regBtn":self.registerBtn}];
    [self addConstraints:layoutB];
    
    self.backgroundColor = [[UIColor alloc] initWithWhite:0.93 alpha:1];
    
    
    
    /*
     
     [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[blueView]-20-|" options:NSLayoutFormatAlignAllBottom metrics:nil views:@{@"blueView" : blueView}];
     
     
     
     */
}


# pragma mark --- 懒加载所有控件
/************************以下所有都是懒加载控件***********************/

// 添加大房子图片
- (UIImageView *)largeView
{

    if (!_largeView) {
        _largeView = [[UIImageView alloc] init];
        
        _largeView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
    }
    return _largeView;
}

//添加背景灰色的图片
- (UIImageView *)backView
{
    
    if (!_backView) {
        _backView = [[UIImageView alloc] init];
        
        _backView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
    }
    return _backView;
}

//添加圆圈图片
- (UIImageView *)cycleView
{
    
    if (!_cycleView) {
        _cycleView = [[UIImageView alloc] init];
        
        _cycleView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
    }
    return _cycleView;
}

//添加下面的lable
- (UILabel *)tipLable
{
    if (!_tipLable) {
        _tipLable = [[UILabel alloc] init];
        
        _tipLable.text = @"关注一些人，回这里看看有什么惊喜";
        _tipLable.textColor = [UIColor darkGrayColor];
        _tipLable.textAlignment = NSTextAlignmentCenter;
        _tipLable.font = [UIFont systemFontOfSize:(14)];
        _tipLable.numberOfLines = 0;
        [_tipLable sizeToFit];
        
    }
    return _tipLable;
}

//添加注册按钮
- (UIButton *)registerBtn
{
    if (!_registerBtn) {
        _registerBtn = [[UIButton alloc] init];
        
        [_registerBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
        _registerBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_registerBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_registerBtn sizeToFit];
    }

    return  _registerBtn;
}

//添加注册按钮
- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [[UIButton alloc] init];
        
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:15.0];
        [_loginBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_loginBtn sizeToFit];
    }
    
    return  _loginBtn;
}


@end
